<template>
  <div v-if="isSuper === 0" style="text-align: center; margin-top: 50px;">
    您的权限不够,请联系管理员升级权限。hzshenpeng@corp.netease.com
  </div>
  <div v-else>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <!-- <el-input v-model="searchModel.username" placeholder="用户名" clearable />
          <el-input v-model="searchModel.phone" placeholder="电话" clearable /> -->
          <el-button type="primary" @click="getCourseList" round icon="el-icon-search">查询</el-button>
        </el-col>
        <el-col v-if="isSuper === 1" :span="4" align="right">
          <el-button @click="editCourse(null)" type="primary" icon="el-icon-plus" circle></el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 注意事项栏 -->
    <el-card>
      <div slot="header" class="clearfix">
        <h1 class="centered-text">科目注意事项</h1>
        <!-- <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button> -->
      </div>
      <div class="centered-text">
        <div class="center">
          <span class="left-span">只有当该科目的(选择和判断)题目数量总和大于等于20道时,模拟训练才能正常进行。</span><br />
        </div>
      </div>
    </el-card>

    <!-- 查询结果栏 -->
    <el-card>
      <el-table :data="course_list" border style="width: 100%">
        <el-table-column label="#" width="100" align="center">
          <template slot-scope="scope">
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="科目名称" align="center">
        </el-table-column>
        <el-table-column prop="desc" label="科目详情"  align="center">
        </el-table-column>
        <el-table-column prop="a_num" label="选择和判断总数"  align="center">
        </el-table-column>
        <el-table-column prop="total_num" label="题目总数"  align="center">
        </el-table-column>
        <el-table-column v-if="isSuper === 1" label="编辑" width="200" align="center">
          <template slot-scope="scope">
            <el-button @click="editCourse(scope.row.id)" type="primary" icon="el-icon-edit" circle
              size="mini"></el-button>
            <el-button @click="open(scope.row)" type="danger" icon="el-icon-delete" circle size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo" :page-sizes="[5, 10, 50, 100]" :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <!-- 弹出框 -->
    <el-dialog @close="clearForm" :title="title" :visible.sync="dialogFormVisible">
      <!-- <el-dialog :title="title" :visible.sync="dialogFormVisible"> -->
      <el-form :model="courseForm">
        <!-- <el-form-item label="科目名称：" prop="course_name" v-if="courseForm.id == null || courseForm.id == undefined"
          :label-width="formLabelWidth">
          <el-input v-model="courseForm.course_name" autocomplete="off"></el-input>
        </el-form-item> -->
        <el-form-item label="科目名称：" prop="name" :label-width="formLabelWidth">
          <el-input v-model="courseForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="科目详情：" prop="desc" :label-width="formLabelWidth">
          <el-input v-model="courseForm.desc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button @click="addCourse" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import testApi from "@/api/testMange";
import { mapGetters } from 'vuex';
export default {
  data() {
    return {
      courseForm: {
        // "idc_name": "",
        // "is_active": 0,
      },
      formLabelWidth: "130px",
      dialogFormVisible: false,
      title: "",
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      userList: [],
      course_list: [],
    };
  },
  methods: {
    open(Course) {
      this.$confirm(`您是否确认删除科目:${Course.name}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          testApi.deleteCourseById(Course.id).then((response) => {
            this.$message({
              type: "success",
              message: response.message,
            });
            this.getCourseList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addCourse() {
      testApi.saveCourse(this.courseForm).then((reponse) => {
        // 关闭弹窗
        this.dialogFormVisible = false;
        // 提示提交成功
        this.$message({
          message: reponse.message,
          type: "success",
        });
        // 刷新页面
        this.getCourseList();
      });
    },
    clearForm() {
      this.courseForm = {
        // "idc_name": "",
        // "is_active": 0,
      };
    },
    editCourse(id) {
      if (id == null) {
        this.title = "新增科目";
      } else {
        this.title = "修改科目";
        //先从后端获取user信息
        testApi.getCourseById(id).then((reponse) => {
          this.courseForm = reponse.data;
        });
      }
      this.dialogFormVisible = true;
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getCourseList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getCourseList();
    },
    getCourseList() {
      testApi.getCourseList(this.searchModel).then((response) => {
        this.total = response.data.total;
        this.course_list = response.data.course_title;
      });
    },
  },
  created() {
    this.getCourseList();
  },
  computed: {
    ...mapGetters([
      // 'name',
      'isSuper',
      // 'nick_name'
    ]),
  }
};
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}

.el-dialog .el-input {
  width: 88%;
}

.centered-text {
  text-align: center;
  /* display: block; */
}

.center {
  display: inline-block;
  text-align: left;
}

.left-span {
  /* text-align: left; */
  display: block;
}
</style>